package com.wnnight.wys.serviceImpl.landlord;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnnight.wys.entity.PageBean;
import com.wnnight.wys.entity.lanlord.Landlord;
import com.wnnight.wys.mapper.landlord.LandlordMapper;
import com.wnnight.wys.service.lanlord.LanlordService;
import com.wnnight.wys.vo.LandlordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Mr.Wang
 * @ClassName: LandlordImpl
 * @Description: 业主实现类
 * @date: 2022/8/29-10:39
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LandlordImpl extends ServiceImpl<LandlordMapper, Landlord> implements LanlordService {
    @Autowired
    private LandlordMapper landlordMapper;

    //业主注册
    @Override
    public void addLandlord(Landlord landlord){
        landlord.setSubTime(DateUtil.now());
        landlord.setStatus("n");
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = bCryptPasswordEncoder.encode(landlord.getPassword());
        landlord.setPassword(password);
//        landlord.setImg("default.jpg");
        landlordMapper.insert(landlord);
    }

    //修改业主账号、密码、头像
    @Override
    public void update(Landlord lanlord) {
        landlordMapper.update(lanlord);
    }

    //业主实名认证
    @Override
    public void realName(Landlord lanlord) {
        lanlord.setSubTime(DateUtil.now());
        landlordMapper.realName(lanlord);
    }

    @Override
    public Landlord getInfo(int id) {
        return landlordMapper.info(id);
    }

    @Override
    public Landlord getImg(int id) {
        return landlordMapper.getImg(id);
    }

    @Override
    public int getNumByColumn(String column, String value) {
        return landlordMapper.getNumByColumn(column,value);
    }

    @Override
    public Landlord doLogin(Landlord lanlord) {
        return landlordMapper.login(lanlord.getAccount(), lanlord.getPassword());
    }

    //业主表多条件分页
    @Override
    public PageBean<List<Landlord>> getByCondition(LandlordVo landlord) {
        int page = landlord.getPage();
        PageBean<List<Landlord>> pageBean = new PageBean<>();
        int pageSize= landlord.getPageSize();
        Page<Landlord> landlordPage = new Page<>(page, pageSize);
        QueryWrapper<Landlord> queryWrapper = new QueryWrapper<>();
        if (landlord.getName() != null && !landlord.getName().trim().equals("")) {
            queryWrapper.like("name",landlord.getName());
        }
        if (landlord.getPhone()!=null && !landlord.getPhone().trim().equals("")) {
            queryWrapper.like("phone",landlord.getPhone());
        }
        if (landlord.getStatus()!=null && !landlord.getStatus().trim().equals("")) {
            queryWrapper.eq("status",landlord.getStatus());
        }
        if (landlord.getPlace()!=null && !landlord.getPlace().trim().equals("")) {
            queryWrapper.like("place",landlord.getPlace());
        }
        if (landlord.getMinSubTime()!=null && !landlord.getMinSubTime().trim().equals("")) {
            queryWrapper.ge("sub_time",landlord.getMinSubTime());
        }
        if (landlord.getMaxSubTime()!=null && !landlord.getMaxSubTime().trim().equals("")) {
            queryWrapper.le("sub_time",landlord.getMaxSubTime());
        }
        IPage<Landlord> landlordIPage = page(landlordPage,queryWrapper);
        pageBean.setData(landlordIPage.getRecords());
        pageBean.setCurrPage(page);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNum((int)landlordIPage.getTotal());
        pageBean.setTotalPages((int)landlordIPage.getPages());
        return pageBean;

    }

    @Override
    public List<Landlord> getByDownload(LandlordVo landlord) {
        QueryWrapper<Landlord> queryWrapper = new QueryWrapper<>();
        if (landlord.getName() != null && !landlord.getName().trim().equals("")) {
            queryWrapper.like("name",landlord.getName());
        }
        if (landlord.getPhone()!=null && !landlord.getPhone().trim().equals("")) {
            queryWrapper.like("phone",landlord.getPhone());
        }
        if (landlord.getStatus()!=null && !landlord.getStatus().trim().equals("")) {
            queryWrapper.eq("status",landlord.getStatus());
        }
        if (landlord.getPlace()!=null && !landlord.getPlace().trim().equals("")) {
            queryWrapper.like("place",landlord.getPlace());
        }
        if (landlord.getMinSubTime()!=null && !landlord.getMinSubTime().trim().equals("")) {
            queryWrapper.ge("sub_time",landlord.getMinSubTime());
        }
        if (landlord.getMaxSubTime()!=null && !landlord.getMaxSubTime().trim().equals("")) {
            queryWrapper.le("sub_time",landlord.getMaxSubTime());
        }
        return landlordMapper.selectList(queryWrapper);

    }

    //逻辑删除，x状态标识已删除
    @Override
    public void deleteLandlord(int id) {
        landlordMapper.deleteLandlord(id);
    }

    @Override
    public Landlord getRoomInfo(int id) {
        return landlordMapper.getRoomInfo(id);
    }

    @Override
    public Landlord getParkInfo(int id) {
        return landlordMapper.getParkInfo(id);
    }

    @Override
    public Landlord getCarInfo(int id) {
        return landlordMapper.getCarInfo(id);
    }
}
